/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.piemenu;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;

import android.os.AsyncTask;
import android.util.Log;

import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;

import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;


import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.Toast;


import com.iiordanov.android.bc.BCFactory;
import com.iiordanov.bVNC.CmsControl;
import com.iiordanov.bVNC.MetaKeyBean;

import com.iiordanov.bVNC.RemoteCanvasActivity;

import com.mogujie.tt.R;

import com.iiordanov.bVNC.dialogs.CmsTextDialog;
import com.iiordanov.bVNC.input.AbstractInputHandler;
import com.iiordanov.bVNC.input.RemoteKeyboard;
import com.iiordanov.bVNC.input.TouchMouseDragPanInputHandler;
import com.iiordanov.bVNC.input.TouchMouseSwipePanInputHandler;


import com.piemenu.view.PieItem;
import com.piemenu.view.PieMenu;
import com.piemenu.view.PieMenu.PieView.OnLayoutListener;
import com.piemenu.view.PieStackView;



/**
 * Controller for Quick Controls pie menu
 */
public class PieControl implements PieMenu.PieController, OnClickListener {

	protected Activity mActivity;

	protected PieMenu mPie;
	protected int mItemSize;
	protected TextView mTabsCount;

	
	private PieItem mKeyboard;
	private PieItem mIn_the_switch;
	private PieItem mSwitch_input;
	private PieItem mVmreturn;
	private PieItem mTeaching;
	private PieItem mSinglemeans;
    
	private  RemoteCanvasActivity mRemoteCanvas;
	private boolean cmsSoftkeyboard =true;

    public PieControl(Activity activity) {
		mActivity = activity;

		mItemSize = (int) activity.getResources().getDimension(
				R.dimen.qc_item_size);
		
		Log.v("PieControl", "The constructor-------------");
    }


    
    public void attachToContainer(FrameLayout container) {//添加扇形菜单
		if (mPie == null) {

			Log.v("PieControl", "attachToContainer--------------" + mPie);
			mPie = new PieMenu(mActivity);
			LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);

			mPie.setLayoutParams(lp);
			populateMenu();
			mPie.setController(this);

		}
		container.addView(mPie);

    }

    protected void removeFromContainer(FrameLayout container) {
        container.removeView(mPie);
    }

    protected void forceToTop(FrameLayout container) {
        if (mPie.getParent() != null) {
            container.removeView(mPie);
            container.addView(mPie);
        }
    }

    protected void setClickListener(OnClickListener listener, PieItem... items) {
        for (PieItem item : items) {
            item.getView().setOnClickListener(listener);
        }
    }

    @Override
    public boolean onOpen() {
      
        return true;
    }

    protected void populateMenu() {
	
		//添加6个功能
		mKeyboard = makeItem(R.drawable.keyboard,1);
		mIn_the_switch = makeItem(R.drawable.in_the_switch,1);
		mSwitch_input = makeItem(R.drawable.switch_input,1);
		mVmreturn = makeItem(R.drawable.vmreturn,1);
		mTeaching = makeItem(R.drawable.teaching,1);
		mSinglemeans = makeItem(R.drawable.singlemeans,1);
		
		PieStackView stack = new PieStackView(mActivity);
		stack.setLayoutListener(new OnLayoutListener() {
			@Override
			public void onLayout(int ax, int ay, boolean left) {

			}
		});

		setClickListener(this, mKeyboard, mIn_the_switch, mSwitch_input, mVmreturn, mTeaching,mSinglemeans);// 监听

		mPie.addItem(mKeyboard);// 添加刷新功能视图
		mPie.addItem(mIn_the_switch);
		mPie.addItem(mSwitch_input);
		mPie.addItem(mVmreturn);
		mPie.addItem(mTeaching);
		mPie.addItem(mSinglemeans);
    }
    
    @Override
    public void onClick(View v) {

		if (mKeyboard.getView() == v) {//开关软键盘
			
			 if(cmsSoftkeyboard)
			  {
			//cms 初始化一个自定义的Dialog
				PageTask task = new PageTask();
		         task.execute();  
		         cmsSoftkeyboard =false;
			  }
			  else
			  {
		         InputMethodManager im = (InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE); // 关闭软键盘
				 im.hideSoftInputFromWindow(mActivity.getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
				 cmsSoftkeyboard =true;
				 mRemoteCanvas.mRemoteCanvasActivity.hideSystemUI(mActivity.getWindow().getDecorView());
			  }
		
			
		//	Toast.makeText(mActivity, "mKeyboard", Toast.LENGTH_SHORT).show();

		} else if (mIn_the_switch.getView() == v) { //中英切换

			RemoteKeyboard k = mRemoteCanvas.mRemoteCanvasActivity.canvas.getKeyboard();
			int key = KeyEvent.KEYCODE_SHIFT_LEFT;//定义发送什么键值										
				BCFactory.getInstance().getBCHaptic()
						.performLongPressHaptic(mRemoteCanvas.mRemoteCanvasActivity.canvas);
		  
				k.repeatKeyEvent(key, new KeyEvent(MotionEvent.ACTION_DOWN, key));//发送键值
			
			//	resetOnScreenKeys(0);
				
				k.stopRepeatingKeyEvent();//停止发送键值
			
		//	Toast.makeText(mActivity, "mIn_the_switch", Toast.LENGTH_SHORT).show();
		} else if (mSwitch_input.getView() == v) {//输入法切换
			
			mRemoteCanvas.mRemoteCanvasActivity.canvas.getKeyboard().sendMetaKey(MetaKeyBean.keyCtrlShift);//发送组合键值  
			//Toast.makeText(mActivity, "mSwitch_input", Toast.LENGTH_SHORT).show();

		} else if (mVmreturn.getView() == v) {//退出VM
			mRemoteCanvas.mRemoteCanvasActivity.canvas.closeConnection();
			mRemoteCanvas.mRemoteCanvasActivity.finish();
			
			
		//	Toast.makeText(mActivity, "mVmreturn", Toast.LENGTH_SHORT).show();

		} else if (mTeaching.getView() == v) {//教学模式
			
			 boolean isRdp = mRemoteCanvas.mRemoteCanvasActivity.getPackageName().contains("RDP");
			    if(CmsControl.switchteachingmode)
				{
				 CmsControl.switchteachingmode = false;
				
				 
				 AbstractInputHandler input = new TouchMouseSwipePanInputHandler((RemoteCanvasActivity)mActivity, mRemoteCanvas.mRemoteCanvasActivity.canvas, isRdp);
				
				 mRemoteCanvas.mRemoteCanvasActivity.inputHandler = input;
				 mRemoteCanvas.mRemoteCanvasActivity.connection.setInputMode(input.getName());
					
				 mRemoteCanvas.mRemoteCanvasActivity.connection.setFollowMouse(true);
				 mRemoteCanvas.mRemoteCanvasActivity.connection.setFollowPan(true);
							
				  Toast.makeText(mActivity, "正常模式", Toast.LENGTH_SHORT).show();
				}
				else
				{
			     CmsControl.switchteachingmode = true;	
			     CmsControl.switchsingledoublesliding = false;
			     
				 AbstractInputHandler input = new TouchMouseDragPanInputHandler((RemoteCanvasActivity)mActivity, mRemoteCanvas.mRemoteCanvasActivity.canvas, isRdp);
				 
				 mRemoteCanvas.mRemoteCanvasActivity.inputHandler = input;
				 mRemoteCanvas.mRemoteCanvasActivity.connection.setInputMode(input.getName());
						
				 mRemoteCanvas.mRemoteCanvasActivity.connection.setFollowMouse(false);
				 mRemoteCanvas.mRemoteCanvasActivity.connection.setFollowPan(false);
						
			     Toast.makeText(mActivity, "教学模式", Toast.LENGTH_SHORT).show();
				}
			    mRemoteCanvas.mRemoteCanvasActivity.showPanningState(true);
			    mRemoteCanvas.mRemoteCanvasActivity.connection.save(mRemoteCanvas.mRemoteCanvasActivity.database.getWritableDatabase());
			    mRemoteCanvas.mRemoteCanvasActivity.database.close();
			
			//Toast.makeText(mActivity, "mTeaching", Toast.LENGTH_SHORT).show();
		} else if (mSinglemeans.getView() == v) {//手指滑动模式
			
			if(CmsControl.switchteachingmode == true) //教学模式下无法切换为单指滑动
			{
				
				CmsControl.switchsingledoublesliding = false;
				Toast.makeText(mActivity, "教学模式下无法切换为单指滑动", Toast.LENGTH_SHORT).show();
			}
			else
			{
			if(CmsControl.switchsingledoublesliding == false)//双指滑动模式
			{
				
				CmsControl.switchsingledoublesliding = true;
				
				Toast.makeText(mActivity, "单指滑动模式", Toast.LENGTH_SHORT).show();
			}
			else //单指滑动模式
			{
				
				CmsControl.switchsingledoublesliding = false;
				
				Toast.makeText(mActivity, "双指滑动模式", Toast.LENGTH_SHORT).show();
			}
			}
			
			//Toast.makeText(mActivity, "mSinglemeans", Toast.LENGTH_SHORT).show();
		}
    }

    protected PieItem makeItem(int image, int l) {//添加菜单条目视图
        ImageView view = new ImageView(mActivity);
        view.setImageResource(image);
        view.setMinimumWidth(mItemSize);
        view.setMinimumHeight(mItemSize);
        view.setScaleType(ScaleType.CENTER);
        LayoutParams lp = new LayoutParams(mItemSize, mItemSize);
        view.setLayoutParams(lp);
        return new PieItem(view, l);//添加到扇形里面
    }

    class PageTask extends AsyncTask<String, Integer, String> {

		 Dialog dialog;
        @Override
        protected void onPreExecute() {
          
       	 dialog = new CmsTextDialog(mActivity);
	         dialog.show();  	
	       
        }

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
				
			return null;
		}
		
		 @Override
	        protected void onPostExecute(String result) {	           
			  try {
					Thread.sleep(100);
					dialog.dismiss();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	}		

}
